﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using ZBJF.Warranty.BLL;
using ZBJF.Warranty.Common.Common;
using ZBJF.Warranty.Model.ViewModel.Sellers;

namespace ZBJF.Warranty.H5.Controllers
{
    public class SellerEmpoyeeController : BaseController
    {
        public ActionResult Index()
        {
            return View();
        }
        public ActionResult Setting()
        {
            return View();
        }
        [LoginFilter(IsCheck = false)]
        public ActionResult ModifyPassword(SellerEmployeeModel model)
        {
            var sellerEmployee = CurrentUser;
            if (model.ModifyPasswordType < 1)
            {
                TempData["success"] = false;
                TempData["Msg"] = string.Format("请从设置页面进入修改密码页面！");
                return RedirectToAction("Setting");
            }
            else if (model.ModifyPasswordType == 1)
            {
                sellerEmployee = ServiceFactory.SellerEmployeeServiceInstance.LoadByPhone(model.UserPhone);
                if (sellerEmployee == null)
                {
                    TempData["success"] = false;
                    TempData["Msg"] = string.Format("用户(手机{0})不存在！", model.UserPhone);
                    return RedirectToAction("ForgetPassword", "Login");
                }
                else
                {
                    var success = ServiceFactory.SendSmsVerificationCodeLogServiceInstance.ValidatePhoneCode(model.UserPhone, model.VerificationCode, 5);
                    if (!success)
                    {
                        TempData["success"] = false;
                        TempData["Msg"] = "您输入的验证码有误或已失效（请在收到验证码后5分钟内使用）！";
                        return RedirectToAction("ForgetPassword", "Login");
                    }
                }
            }
            if (sellerEmployee == null)
            {
                RedirectToAction("Index", "Login");
            }
            else
            {
                model.ID = sellerEmployee.ID;
                model.UserPhone = sellerEmployee.UserPhone;
            }
            return View(model);
        }
        [ValidateInput(true)]
        [ValidateAntiForgeryToken]
        [HttpPost]
        [LoginFilter(IsCheck = false)]
        public ActionResult SaveModifyPassword(SellerEmployeeModel model)
        {
            var result = false;
            if (model.ModifyPasswordType == 2)
            {
                if (!ServiceFactory.SellerEmployeeServiceInstance.ValidatePassword(model.ID, model.Password))
                {
                    TempData["success"] = false;
                    TempData["Msg"] = "旧密码错误，请输入正确的旧密码！";
                    return View("ModifyPassword", model);
                }
            }
            result = ServiceFactory.SellerEmployeeServiceInstance.ModifyPassword(model.ID, model.NewPassword,
                model.UserPhone);
            TempData["msg"] = result ? "密码修改成功" : "密码修改失败";
            TempData["success"] = result;
            if (result)
            {
                return RedirectToAction("LogOut", "Login");
            }
            return View("ModifyPassword", model);
        }

    }
}